package com.bjsxt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjsxt.mapper.RoleMapper;
import com.bjsxt.mapper.RoleMenuMapper;
import com.bjsxt.pojo.*;
import com.bjsxt.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-10-14
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {
    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Override
    public PageResult<Role> selRoleInfo(Integer page, Integer rows) {
        IPage<Role> iPage = new Page<>(page,rows);
        IPage<Role> iPage1 = baseMapper.selectPage(iPage, null);
        //获取分页的角色信息
        List<Role> records = iPage1.getRecords();
        //遍历获取角色菜单ID的集合
        for(Role r : records){
            //创建条件构造器
            QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("mid").eq("rid",r.getRid());
            List<Object> objects = roleMenuMapper.selectObjs(queryWrapper);
            //将菜单ID的集合存储到角色中
            r.setMids(objects);
        }
        PageResult<Role> pageResult = new PageResult<>(iPage1.getTotal(),iPage1.getRecords());
        return pageResult;
    }

    //增加角色
    @Override
    public HostAddResult insRoleInfoService(Role role, String mids) {
        //增加角色信息
        System.out.println(role);
        System.out.println(mids);
        int insert = baseMapper.insert(role);

        //增加角色和菜单的关联
        if(!"".equals(mids) && mids!=null){
            String[]midStr= mids.split(",");
            //遍历完成增加
            for(String mid:midStr){
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setRid(role.getRid());
                roleMenu.setMid(Integer.parseInt(mid));
                System.out.println("----------");
                System.out.println(roleMenu);
                boolean insert1 = roleMenu.insert();
                System.out.println(insert1);

            }
        }

        return new HostAddResult(insert>0?true:false,"");
    }

    //更新角色
    @Override
    public HostAddResult upRoleInfoService(Role role, String mids) {
        String[]midStr = mids.split(",");
        //更新角色信息
        boolean b = role.updateById();
        //更新菜单权限信息
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("rid",role.getRid());
        int delete = roleMenuMapper.delete(queryWrapper);
        for (String m:midStr){
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setMid(Integer.parseInt(m));
            roleMenu.setRid(role.getRid());
            roleMenu.insert();
        }

        return new HostAddResult(b,"");
    }

    @Override
    public HostAddResult delRoleInfoService(String rids) {
        String[] midStr = rids.split(",");

        QueryWrapper queryWrapper = new QueryWrapper();

        RoleMenu roleMenu = new RoleMenu();
        AdminRole adminRole = new AdminRole();
        for (String r:midStr){
            queryWrapper.eq("rid",r);
//            roleMenu.setRid(Integer.parseInt(r));

        }
        roleMenu.delete(queryWrapper);
        adminRole.delete(queryWrapper);

        int delete = baseMapper.delete(queryWrapper);


        return new HostAddResult(delete>0,"");
    }
}
